package com.china08.yxyapi.api.manage.tec.teaching.homework.myques;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkQuesGroup;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkQuesGroupRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class HomeworkGroupCrtController extends YxyApiBaseController {

    @Autowired
    private HomeworkQuesGroupRepository homeworkQuesGroupRepository;

    @ApiOperation(value = "创建试题组")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/mysql/group/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        HomeworkQuesGroup homeworkQuesGroup = new HomeworkQuesGroup();
        YxyProfile profile = getProfile(request, response);
        String schoolId = profile.getSchoolId();
        String teacherId = profile.getId();
        long check = homeworkQuesGroupRepository.countBySchoolIdAndTeacherIdAndStageAndSubject(teacherId, teacherId,
                reqModel.phase, reqModel.subject);
        Assert.isTrue(check <= 10, "分组最大数量为10");
        homeworkQuesGroup.setSchoolId(schoolId);
        homeworkQuesGroup.setTeacherId(teacherId);
        homeworkQuesGroup.setStage(reqModel.phase);
        homeworkQuesGroup.setSubject(reqModel.subject);
        homeworkQuesGroup.setGroupName(reqModel.name);
        homeworkQuesGroup = homeworkQuesGroupRepository.save(homeworkQuesGroup);
        RespModel respModel = new RespModel();
        respModel.quesCount = 0;
        respModel.name = homeworkQuesGroup.getGroupName();
        respModel.id = homeworkQuesGroup.getId();
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String phase;
        public String subject;
        public String name;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(name, null), "请填写分组名称");
            Assert.isTrue(StringUtils.length(name) <= 10, "分组名称必须小于等于10");
        }
    }

    protected static class RespModel {
        public String name;
        public String id;
        public Integer quesCount;

    }
}
